// 注：这个文件被用于管理项目中所有与“用户管理模块”有关的接口
import request from '@/utils/request'
import type {
    typeOfResponseDataOfTheApiThatGetUserList,
    typeOfObjectThatStoreUserInformation,
    typeOfResponseDataOfTheApiThatGetrequestAllOfPositionsAndPositionsThatCurrentUserHaveBeenHad,
    typeOfParamsThatSentToTheApiThatAssignPositionsToAUser,
} from '@/api/acl/user/type'
// 枚举地址↓
enum API {
    // 获取所有用户信息的接口地址↓
    getAllOfUsersInformation_URL = '/admin/acl/user/',
    // 添加用户的接口地址↓
    addAUser_URL = '/admin/acl/user/save',
    // 更新用户的接口地址↓
    updateAUser_URL = '/admin/acl/user/update',
    // 获取所有职位信息和当前用户已经拥有的职位信息时需要用到的接口地址↓
    GetAllOfPositionsAndPositionsThatCurrentUserHaveBeenHad_URL = '/admin/acl/user/toAssign/',
    // 为用户分配职位的接口地址↓
    assignPositionForAUser_URL = '/admin/acl/user/doAssignRole/',
    // 删除某一个用户的接口地址↓
    deleteAUser_URL = '/admin/acl/user/remove/',
    // 批量删除用户的接口地址↓
    deleteALotOfUser_URL = '/admin/acl/user/batchRemove',
}
/**
 * @description 函数“requestAllOfUsersInformation”被用于“请求获取所有用户信息”
 * @param pageNumber 页码（默认从1开始，往后递增）
 * @param pageSize 每页显示多少条数据
 * @returns Promise<any> 返回一个promise对象，它的解析结果中包含了用户信息列表、总记录数等信息
 */
export const requestAllOfUsersInformation = async (pageNumber: number, pageSize: number,username:string) => {
    try {
        let response = await request.get<any, typeOfResponseDataOfTheApiThatGetUserList>(API.getAllOfUsersInformation_URL + `${pageNumber}/${pageSize}?username=${username}`)
        console.log(response)
        return response
    } catch (error: any) {
        console.error("获取用户信息失败", error)
    }
}
/**
 * @description 函数“requestAddOrUpdateAUser”被用于请求添加或更新用户信息
 * @param userInfo 用户信息对象，包含了用户的所有信息↓
 * @returns Promise<any> 返回一个promise对象，包含了请求的结果信息
 */
export const requestAddOrUpdateAUser = async (userInfo: typeOfObjectThatStoreUserInformation) => {
    // 如果携带了用户ID，则认为是更新用户信息操作↓
    if (userInfo.id) {
        try {
            let response = await request.put<any, any>(API.updateAUser_URL, userInfo)
            console.log(response)
            return response
        } catch (error: any) {
            console.error("更新用户信息失败", error)
        }
    } else {
        try {
            let response = await request.post<any, any>(API.addAUser_URL, userInfo)
            console.log(response)
            return response
        } catch (error: any) {
            console.error("添加用户失败", error)
        }
    }

}
/**
 * @description 函数“requestAllOfPositionsAndPositionsThatCurrentUserHaveBeenHad”被用于请求获取所有职位和当前用户已经拥有的职位
 */
export const requestAllOfPositionsAndPositionsThatCurrentUserHaveBeenHad = async (userId: number) => {
    try {
        let response = await request.get<any, typeOfResponseDataOfTheApiThatGetrequestAllOfPositionsAndPositionsThatCurrentUserHaveBeenHad>(API.GetAllOfPositionsAndPositionsThatCurrentUserHaveBeenHad_URL + userId)
        console.log(response)
        return response
    } catch (error: any) {
        console.error("获取所有职位和当前用户已经拥有的职位失败", error)
    }

}
/**
 * @description 函数“requestAssignPositionForAUser”被用于请求为用户分配职位
 */
export const requestAssignPositionForAUser = async (data:typeOfParamsThatSentToTheApiThatAssignPositionsToAUser) => {
    try {
        let response = await request.post<any,any>(API.assignPositionForAUser_URL, data)
        console.log(response)
        return response
    } catch (error: any) {
        console.error("为用户分配职位失败", error)
    }
}
/**
 * @description 函数“requestDeleteAUser”被用于请求删除一个用户（删除用户的账号）
 * @param userId 被删除的用户的ID
 */
export const requestDeleteAUser = async (userId: number) => {
    try {
        let response = await request.delete<any, any>(API.deleteAUser_URL + userId)
        console.log(response)
        return response
    } catch (error: any) {
        console.error("删除用户失败", error)
    }
}
/**
 * @description 函数“requestDeleteALotOfUser”被用于请求批量删除用户（删除用户的账号）
 * @param idList 用户ID列表数组，用于批量删除用户账号账号操作（例如：“[1,2,3]”表示删除ID为“1、2 和 3”的用户账号）
 */
export const requestDeleteALotOfUser = async (idList:number[]) => {
    try {
        let response = await request.delete<any, any>(API.deleteALotOfUser_URL,{data:idList})
        console.log(response)
        return response
    } catch (error: any) {
        console.error("批量删除用户失败", error)
    }
}